﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Smled
{
    public partial class TileChooser : UserControl
    {
        private TileSet mSet;
        private int vTiles;
        private int offset = 0;
        private int sel = 0;
        private bool nulTile = false;
        private Orientation orient = Orientation.Vertical;

        public TileChooser()
        {
            InitializeComponent();
            this.Resize += new EventHandler(TileChooser_Resize);
            this.Paint += new PaintEventHandler(TileChooser_Paint);
            //this.sBar.ValueChanged += new EventHandler(sBar_ValueChanged);
            this.MouseClick += new MouseEventHandler(TileChooser_MouseClick);
            this.MouseWheel += new MouseEventHandler(TileChooser_MouseWheel);
            vTiles = this.Height / 32;
        }

        public Orientation ChooserOrientation
        {
            get
            {
                return orient;
            }
            set
            {
                orient = value;
                setupOrientation();
                this.Invalidate();
            }
        }

        private void setupOrientation()
        {
            if (orient == Orientation.Vertical)
            {
                vTiles = this.Height / 32;
                this.Width = 32;
                this.MaximumSize = new Size(32, 0);
            }
            else
            {
                vTiles = this.Width / 32;
                this.Height = 32;
                this.MaximumSize = new Size(0, 32);
            }
        }

        void TileChooser_MouseWheel(object sender, MouseEventArgs e)
        {
            offset += (e.Delta < 0) ? 1 : -1;
            if (mSet.Set.Count - vTiles < offset)
            {
                offset = mSet.Set.Count - vTiles;
            }
            if (offset < 0) offset = 0;
            this.Invalidate();
        }

        void TileChooser_MouseClick(object sender, MouseEventArgs e)
        {
            if (mSet != null)
            {
                int oSel = sel;
                
                if (orient == Orientation.Vertical)
                    sel = e.Y / 32 + offset;
                else
                    sel = e.X / 32 + offset;

                if (sel == oSel && !nulTile)
                {
                    nulTile = true;
                }
                else
                {
                    nulTile = false;
                    if (sel >= mSet.Set.Count) sel = mSet.Set.Count - 1;
                    if (sel < 0) sel = 0;
                }
                this.Invalidate();
            }
        }

        //void sBar_ValueChanged(object sender, EventArgs e)
        //{
        //    offset = sBar.Value;
        //    this.Invalidate();
        //}

        void TileChooser_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (mSet == null) { g.Clear(this.BackColor); return; }

            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, 32, 32), 
                Color.FromArgb(96, 96, 96, 96), Color.FromArgb(96, 0, 0, 0), System.Drawing.Drawing2D.LinearGradientMode.Vertical);
            Brush b2 = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, 32, 32),
                Color.FromArgb(0, 0, 0), Color.FromArgb(16, 16, 16), System.Drawing.Drawing2D.LinearGradientMode.Vertical);


            if (orient == Orientation.Vertical)
            {
                for (int i = 0; i <= vTiles; ++i)
                {
                    if (i + offset < mSet.Set.Count && mSet.Set[i + offset].TileImage != null)
                    {
                        g.DrawImageUnscaled(mSet.Set[i + offset].TileImage, 0, i * 32);
                        if (nulTile || sel != i + offset)
                        {
                            g.FillRectangle(b, 0, i * 32, 32, 32);
                        }
                        else
                        {
                            g.DrawRectangle(Pens.Yellow, 0, i * 32, 31, 31);
                        }
                    }
                    else
                    {
                        g.FillRectangle(b2, 0, i * 32, 32, 32);
                    }
                }
            }
            else
            {
                for (int i = 0; i <= vTiles; ++i)
                {
                    if (i + offset < mSet.Set.Count && mSet.Set[i + offset].TileImage != null)
                    {
                        g.DrawImageUnscaled(mSet.Set[i + offset].TileImage, i * 32, 0);
                        if (nulTile || sel != i + offset)
                        {
                            g.FillRectangle(b, i * 32, 0, 32, 32);
                        }
                        else
                        {
                            g.DrawRectangle(Pens.Yellow, i * 32, 0, 31, 31);
                        }
                    }
                    else
                    {
                        g.FillRectangle(b2, i * 32, 0, 32, 32);
                    }
                }
            }
        }

        void TileChooser_Resize(object sender, EventArgs e)
        {
            if (orient == Orientation.Vertical)
                vTiles = this.Height / 32;
            else
                vTiles = this.Width / 32;

            if (mSet != null)
            {
                int scr = mSet.Set.Count - vTiles;
                if (scr < 0) scr = 0;
                //sBar.Maximum = scr;
            }
            else
            {
                //sBar.Maximum = 0;
            }
            //sBar.Minimum = 0;
            //sBar.Enabled = sBar.Maximum > 0;
            //sBar.Refresh();
            this.Invalidate();
        }

        public TileSet Set
        {
            get { return mSet; }
            set 
            { 
                mSet = value;
                this.Invalidate();
            }
        }

        public Tile SelectedTile
        {
            get
            {
                if (mSet == null || nulTile) return null;
                return mSet.Set[sel];
            }
        }

        public void NulToggle()
        {
            nulTile = !nulTile;
            this.Invalidate();
        }

        public void NextTile()
        {
            if (mSet == null) return;
            sel++;
            nulTile = false;
            if (sel >= mSet.Set.Count)
            {
                sel = 1;
                PreviousTile();
            }
            if (sel >= offset + vTiles) offset = sel - vTiles + 1;
            this.Invalidate();
        }
        
        public void PreviousTile()
        {
            if (mSet == null) return;
            sel--;
            nulTile = false;
            if (sel < 0)
            {
                sel = mSet.Set.Count - 2;
                NextTile();
            }
            if (sel < offset) offset = sel;
            this.Invalidate();
        }

    }
}
